package me.zhengjie.modules.ImgIntroduction.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import me.zhengjie.base.PageInfo;
import me.zhengjie.base.QueryHelpMybatisPlus;
import me.zhengjie.base.impl.CommonServiceImpl;
import me.zhengjie.utils.ConvertUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.modules.ImgIntroduction.domain.ImageIntroduction;
import me.zhengjie.modules.ImgIntroduction.service.ImageIntroductionService;
import me.zhengjie.modules.ImgIntroduction.service.dto.ImageIntroductionDto;
import me.zhengjie.modules.ImgIntroduction.service.dto.ImageIntroductionQueryParam;
import me.zhengjie.modules.ImgIntroduction.service.mapper.ImageIntroductionMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author TianI
* @date 2022-01-23
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = ImageIntroductionService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ImageIntroductionServiceImpl extends CommonServiceImpl<ImageIntroductionMapper, ImageIntroduction> implements ImageIntroductionService {

    // private final RedisUtils redisUtils;
    private final ImageIntroductionMapper imageIntroductionMapper;

    @Override
    public PageInfo<ImageIntroductionDto> queryAll(ImageIntroductionQueryParam query, Pageable pageable) {
        IPage<ImageIntroduction> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<ImageIntroduction> page = imageIntroductionMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, ImageIntroductionDto.class);
    }

    @Override
    public List<ImageIntroductionDto> queryAll(ImageIntroductionQueryParam query){
        return ConvertUtil.convertList(imageIntroductionMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), ImageIntroductionDto.class);
    }

    @Override
    public ImageIntroduction getById(Long id) {
        return imageIntroductionMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public ImageIntroductionDto findById(Long id) {
        return ConvertUtil.convert(getById(id), ImageIntroductionDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(ImageIntroductionDto resources) {
        ImageIntroduction entity = ConvertUtil.convert(resources, ImageIntroduction.class);
        return imageIntroductionMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(ImageIntroductionDto resources){
        ImageIntroduction entity = ConvertUtil.convert(resources, ImageIntroduction.class);
        int ret = imageIntroductionMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Long> ids){
        // delCaches(ids);
        return imageIntroductionMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Long id){
        Set<Long> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Long id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Long> ids) {
        for (Long id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<ImageIntroductionDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (ImageIntroductionDto imageIntroduction : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("房产项目id", imageIntroduction.getRealEstateProjectId());
              map.put("图片url", imageIntroduction.getImageUrl());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
